import 'package:enterprise_v6_app/app/components/custom_modele/custom_intrinsic_circle_line.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_title_chart_page.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/ext.dart';
import 'package:enterprise_v6_app/app/modules/task/task_all/hazard_investigation/annual_hazard_plan/annual_hazard_plan_detail/annual_hazard_planinfo/annual_hazard_planinfo_controller.dart';

///基本信息
class AnnualHazardInvestigationPlanInfoPage extends StatelessWidget {
  AnnualHazardInvestigationPlanInfoPage({super.key});

  final controller = Get.put(AnnualHazardInvestigationPlanInfoController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: AppColors.bg_color_grey,
      body: SingleChildScrollView(
        child: Column(
          children: [_topWidget(), _bottomWidget()],
        ),
      ),
    );
  }

  Widget _rowData(String name, String value, {MainAxisAlignment mainAxisAlignment = MainAxisAlignment.spaceBetween}) {
    return Row(
      mainAxisAlignment: mainAxisAlignment,
      children: [
        Text(
          name,
          style: TextStyle(
            color: const Color(0xFFA5A6B5),
            fontWeight: FontWeight.w400,
            fontSize: 12.sp,
          ),
        ),
        Expanded(
            child: Text(
          value,
          style: TextStyle(
            color: const Color(0xFF495061),
            fontSize: 12.sp,
            fontWeight: FontWeight.w400,
          ),
        )),
      ],
    );
  }

  Widget _topTitle(String title, Color color, String? value, Color square) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.center,
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        Text(
          title,
          style: TextStyle(
            fontSize: 14.sp,
            color: color,
            height: 1.2,
            fontWeight: FontWeight.w400,
          ),
        ),
        AppGaps.hGap12,
        Text(
          value ?? '空',
          style: TextStyle(
            fontSize: 16.sp,
            color: square,
            fontWeight: FontWeight.w500,
          ),
        ),
      ],
    );
  }

  ///分割线
  Widget _divider() {
    return Container(
      width: 1.w,
      height: 24.w,
      decoration: const BoxDecoration(
          gradient: LinearGradient(begin: Alignment.topCenter, end: Alignment.bottomCenter, colors: [
        Color(0xFFFFFFFF),
        Color(0xFFFFFFFF),
      ])),
    );
  }

  Widget _trouble(Color color, String title, String? value) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.center,
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Text(
          title,
          style: TextStyle(
            color: AppColors.font_color_blue,
            fontWeight: FontWeight.w400,
            fontSize: 12.sp,
          ),
        ),
        AppGaps.vGap2,
        Text(
          value.toString(),
          style: TextStyle(
            color: color,
            fontWeight: FontWeight.w500,
            fontSize: 16.sp,
          ),
        ),
      ],
    );
  }

  Widget _itemWidget(int index) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        AppGaps.hGap5,
        Expanded(
          child: Container(
            padding: EdgeInsets.symmetric(vertical: 10.w),
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(8),
              color: const Color(0xFFF5F6FF),
              border: Border.all(
                color: const Color(0x33587DF5),
              ),
            ),
            child: Column(
              children: [
                Row(
                  children: [
                    AppGaps.hGap12,
                    Text(
                      '${controller.getPlanRecordEntity[index].riskUnitName}',
                      style: TextStyle(color: const Color(0xFF495061), fontWeight: FontWeight.w500, fontSize: 14.sp),
                    ),
                  ],
                ),
                AppGaps.vGap10,
                Row(
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: [
                    AppGaps.hGap12,
                    Row(
                      children: [
                        _trouble(const Color(0xFF5C6DFF), '任务', '${controller.getPlanRecordEntity[index].taskNum ?? '0'}'),
                      ],
                    ),
                  ],
                ),
              ],
            ),
          ),
        )
      ],
    );
  }

  Widget _topWidget() {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 16.w),
      color: AppColors.bg_color_grey,
      child: CustomTitleChartPage(
        mainAxisSize: MainAxisSize.min,
        title: controller.hiddenDangerInvestigationLedgerRecords.planName ?? '',
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            _rowData('排查类型：', controller.hiddenDangerInvestigationLedgerRecords.inspectionTypeName ?? ''),
            AppGaps.vGap12,
            _rowData('排查部门：',
                '${controller.hiddenDangerInvestigationLedgerRecords.inspectionDepartmentName ?? '空'}-${controller.hiddenDangerInvestigationLedgerRecords.inspectionPersonnelName ?? '空'}'),
            AppGaps.vGap12,
            Container(
              padding: EdgeInsets.symmetric(vertical: 10.w),
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(8),
                color: const Color(0x1A5C6DFF),
              ),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: [
                  Column(
                    children: [
                      _topTitle(
                          '点位', const Color(0xFF495061), '${controller.hiddenDangerInvestigationLedgerRecords.numberOfPoints ?? '0'}', const Color(0xFF5C6DFF)),
                    ],
                  ),
                  _divider(),
                  Column(
                    children: [
                      _topTitle('任务', const Color(0xFF495061), '${controller.hiddenDangerInvestigationLedgerRecords.taskNum ?? '0'}', const Color(0xFF5C6DFF)),
                    ],
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _bottomWidget() {
    return GetBuilder(
        init: controller,
        builder: (_) {
          return controller.getPlanRecordEntity.isNotEmpty
              ? Container(
                  margin: EdgeInsets.only(bottom: 120.w),
                  padding: EdgeInsets.symmetric(vertical: 10.w, horizontal: 16.w),
                  color: AppColors.bg_color_grey,
                  child: Container(
                    decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.circular(10.w),
                    ),
                    child: Container(
                      padding: EdgeInsets.fromLTRB(16.w, 20.w, 16.w, 0.w),
                      child: Column(
                        children: [
                          ListView.builder(
                            physics: const ClampingScrollPhysics(),
                            shrinkWrap: true,
                            itemCount: controller.getPlanRecordEntity.length,
                            itemBuilder: (context, index) {
                              final item = controller.getPlanRecordEntity[index];
                              return CustomIntrinsicCircleLine(
                                reverse: true,
                                iconSpaceHeight: 0.w,
                                iconWidget: Container(
                                  width: 10.w,
                                  height: 10.w,
                                  decoration: BoxDecoration(
                                    shape: BoxShape.circle,
                                    border: Border.all(
                                      color: _color(index % 4),
                                      width: 2.w,
                                    ),
                                  ),
                                ),
                                showWidget: Container(
                                  margin: EdgeInsets.only(bottom: 20.w),
                                  child: _itemWidget(index).click(() {
                                    return controller.onClickDetailDetail(item);
                                  }),
                                ),
                              );
                            },
                          ),
                        ],
                      ),
                    ),
                  ),
                )
              : Container();
        });
  }
}

Color _color(int index) {
  switch (index) {
    case 1:
      return const Color(0xFFFD6B6B);
    case 2:
      return const Color(0xFFFC9900);
    case 3:
      return const Color(0xFF5C6DFF);
    default:
      return const Color(0xFFFD6B6B);
  }
}
